1525B - Permutation Sort - CodeForces Solution


constructive algorithms greedy *900

Please click on ads to support us..

Python Code:

for run in range(int(input())):
    n=int(input())
    lst=list(map(int,input().split()))
    cpy=lst.copy()
    cpy.sort()
    if lst==cpy:
        print(0)
    elif lst[0]==1 or lst[-1]==n:
        print(1)
    elif lst[0] == n and lst[-1] ==1:
        print(3)
    else:
        print(2)

C++ Code:

#include<bits/stdc++.h>
using namespace std;
int a[101];
int b[100];
int main()
{
	int t;
	cin >> t;
	for (int l=0; l<t; ++l)
    {
        int n, v=2;
        cin >> n;
        for (int i=1; i<=n; ++i)
        {
            cin >> a[i];
            b[i]=a[i];
        }
        sort(b+1, b+n+1);
        bool f=true;
        for (int i=1; i<=n; ++i)
        {
            if (a[i]!=b[i]) {f=false; break;}
        }
        if (f) {cout << "0 \n"; continue;}
        else if (a[1]==1 || a[n]==n) {cout << "1 \n"; continue;}
        else if (a[1]==n && a[n]==1) {cout << "3 \n"; continue;}
        cout << "2 \n";
	}
}


Comments

Submit
0 Comments
More Questions

844B - Rectangles
1591A - Life of a Flower
1398C - Good Subarrays
629A - Far Relative’s Birthday Cake
1166A - Silent Classroom
1000B - Light It Up
218B - Airport
1463B - Find The Array
1538C - Number of Pairs
621B - Wet Shark and Bishops
476B - Dreamoon and WiFi
152C - Pocket Book
1681D - Required Length
1725D - Deducing Sortability
1501A - Alexey and Train
721B - Passwords
1263D - Secret Passwords
1371B - Magical Calendar
1726E - Almost Perfect
1360C - Similar Pairs
900A - Find Extra One
1093D - Beautiful Graph
748A - Santa Claus and a Place in a Class
1511B - GCD Length
676B - Pyramid of Glasses
597A - Divisibility
1632A - ABC
1619D - New Year's Problem
242B - Big Segment
938A - Word Correction